package com.sctvcloud.longchang.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.canyinghao.canrefresh.CanRefreshLayout;
import com.ruihang.generalibrary.utils.GroupOrderHelper;
import com.ruihang.generalibrary.utils.ListUtils;
import com.sctvcloud.longchang.R;
import com.sctvcloud.longchang.application.Constances;
import com.sctvcloud.longchang.base.BaseActivity;
import com.sctvcloud.longchang.beans.ARecomdList;
import com.sctvcloud.longchang.beans.ActivityBean;
import com.sctvcloud.longchang.beans.ActivityVo;
import com.sctvcloud.longchang.beans.FocusColumn;
import com.sctvcloud.longchang.http.AbsNetCallBack;
import com.sctvcloud.longchang.http.NetUtils;
import com.sctvcloud.longchang.ui.activities.CountyNewsDetailActivity;
import com.sctvcloud.longchang.ui.activities.H5NormalActivity;
import com.sctvcloud.longchang.ui.activities.LivingActivity;
import com.sctvcloud.longchang.ui.activities.NewesDetailActivity;
import com.sctvcloud.longchang.ui.activities.TopicDetailActivity;
import com.sctvcloud.longchang.ui.adapter.ActivityAdapter;
import com.sctvcloud.longchang.ui.adapter.LiveHomeAdapter;
import com.sctvcloud.longchang.ui.adapter.holder.LiveHomeItemHolder;
import com.sctvcloud.longchang.ui.adapter.holder.OnlyTitleHolder;
import com.sctvcloud.longchang.ui.adapter.holder.TvHolder;
import com.sctvcloud.longchang.ui.util.IListShowData;
import com.sctvcloud.longchang.ui.utils.LiveHomeDataAddHelper;
import com.sctvcloud.longchang.ui.utils.LiveListGetHelper;
import com.sctvcloud.longchang.ui.utils.SkipUtil;
import com.sctvcloud.longchang.ui.widget.BasePlayerView;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 活动栏目
 */
public class ActivityFragment extends ColumnBaseFragment implements CanRefreshLayout.OnRefreshListener, CanRefreshLayout.OnLoadMoreListener, ActivityAdapter.OnItemBtnClick {

    @BindView(R.id.fm_live_refresh)
    protected CanRefreshLayout refreshLayout;
    @BindView(R.id.can_content_view)
    protected RecyclerView recyclerView;

    private int currentIndex = 0;
    private int pageAll;
    private ActivityAdapter adapter;
    private List<ActivityBean> dataList = new ArrayList<>();


    public ActivityFragment() {
        // Required empty public constructor
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_activity, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    protected void initViews(View v, @Nullable Bundle savedInstanceState) {
        refreshLayout.setOnLoadMoreListener(this);
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setLoadMoreEnabled(false);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        adapter = new ActivityAdapter(getActivity(), null, this);
        recyclerView.setAdapter(adapter);
        refreshLayout.autoRefresh();
    }

    @Override
    public void onLoadMore() {
        currentIndex++;
        getData();
    }

    @Override
    public void onRefresh() {
        dataList.clear();
        currentIndex = 0;
        getData();
    }

    @Override
    protected void onFragmentUnShowed(boolean isCallFromPause) {
        super.onFragmentUnShowed(isCallFromPause);
        if (null != recyclerView) {

        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden && null != recyclerView) {

        }
    }


    private void getData() {
        NetUtils.getNetAdapter().getActivityList(getOwnerName(), channelUrl, currentIndex, new AbsNetCallBack<ActivityVo>(ActivityVo.class, currentIndex) {
            @Override
            public void onSuc(ActivityVo vo) {
                if (vo != null) {
                    pageAll = vo.getPageAll();
                    currentIndex = vo.getPageIndex();
                    setData(vo.getNewsList());
                }
            }

            @Override
            public void onError(Throwable ex, String errorMsg) {
                super.onError(ex, errorMsg);
                currentIndex--;
            }

            @Override
            public void onEnd() {
                super.onEnd();
                onNetFinish();
            }
        });


    }

    private void setData(List<ActivityBean> list) {
        if (ListUtils.isListValued(list)) {
            dataList.addAll(list);
            adapter.setDataList(dataList);
        }
    }

    private synchronized void onNetFinish() {
        if (refreshLayout.isRefreshing()) {
            refreshLayout.refreshComplete();
        }
        if (refreshLayout.isLoading()) {
            refreshLayout.loadMoreComplete();
        }
        refreshLayout.setLoadMoreEnabled(currentIndex + 1 < pageAll);
    }

    @Override
    public void onItemBtnClick(ActivityBean bean) {
        Intent intent = null;
        switch (bean.getNewsType()) {
            case Constances.NEWS_TYPE_GRAPHIC:
            case Constances.NEWS_TYPE_AUDIO:
            case Constances.NEWS_TYPE_VIDEO:
                //新闻详情
                intent = new Intent(getContext(), NewesDetailActivity.class);
                intent.putExtra(BaseActivity.EX_URL, bean.getNewsUrl());
                break;
            case Constances.NEWS_TYPE_TOPIC:
                //专题详情
                intent = new Intent(getContext(), TopicDetailActivity.class);
                intent.putExtra(BaseActivity.EX_URL, bean.getNewsUrl());
                break;
            case Constances.NEWS_TYPE_LIVE:
                //直播详情
                intent = new Intent(getContext(), LivingActivity.class);
                intent.putExtra(BaseActivity.EX_URL, bean.getNewsUrl());
                intent.putExtra(BaseActivity.EX_DATA, bean.getNewsImage());
                break;
            case Constances.NEWS_TYPE_H5:
                //H5
                intent = new Intent(getContext(), H5NormalActivity.class);
                intent.putExtra(H5NormalActivity.EX_HTML, bean.getNewsUrl());
                intent.putExtra(H5NormalActivity.EX_TITLE, bean.getNewsTitle());
                intent.putExtra(H5NormalActivity.H5_NEWS_ID, bean.getNewsId());
                break;
            case Constances.NEWS_TYPE_COUNTY:
                //区县新闻详情
                intent = new Intent(getContext(), CountyNewsDetailActivity.class);
                intent.putExtra(BaseActivity.EX_URL, bean.getNewsUrl());
                break;
        }
        if (intent != null) {
            intent.putExtra("TAG", tag);
            startActivity(intent);
        }
    }
}